#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="index"
__date__="2016/7/7"
__time__="10:06"
"""
import sys
from model.user import User
import SocketServer
from model.record import Record
import time

class MyServer(SocketServer.BaseRequestHandler):
    def setup(self):
        pass
    def handle(self):
        # print self.request  # 客户端socket对象
        # print self.client_address   # 客户端ip和端口
        # print self.server   # 服务端socketserver 对象
        while True:
            conn = self.request
            user=conn.recv(1024)
            passwd=conn.recv(1024)
            people = User()
            result = people.Check_login(user,passwd)
            if result == None:
                conn.send('login fail !! please check your name and password')
                continue
            else:
                conn.send('Login sucess !! welcome !!!')
                id = people.get_id(user)
                break

        flag = True
        while flag:
            R=Record()
            data = conn.recv(1024)
            print data
            if data == 'exit':
                flag = False
            elif data == 'record':
                RL=R.Get_record(id['id'])
                conn.send(str(RL))
                continue

            send_data=raw_input('robot:')
            conn.send(send_data)
            time_stamp= time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
            content=('%s:'+str(data) +';'+ 'robot:'+str(send_data) ) % user

            R.Insert_record(id['id'],content,time_stamp)

        conn.close()
    def finish(self):
        pass
if __name__ == '__main__':
    server = SocketServer.ThreadingTCPServer(('127.0.0.1',9999),MyServer)
    server.serve_forever()

